﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Proy200Millas_BE;

namespace Proy200Millas_ADO
{
    public class BarcoADO
    {
        ConexionADO MiConexion = new ConexionADO();
        SqlConnection cnx = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dtr = default(SqlDataReader);
        Boolean vexito = false;

        public DataTable ListarBarco()
        {
            DataSet dts = new DataSet();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_Listar_Barco";
                SqlDataAdapter miada = default(SqlDataAdapter);
                miada = new SqlDataAdapter(cmd);
                miada.Fill(dts, "Barco");

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return dts.Tables["Barco"];
        }

        public bool InsertarBarco(BarcoBE _barcoBE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_InsertarBarco";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vnom_bar", SqlDbType.VarChar, 15));
                cmd.Parameters["@vnom_bar"].Value = _barcoBE.Nom_bar;
                cmd.Parameters.Add(new SqlParameter("@vfec_com", SqlDbType.SmallDateTime));
                cmd.Parameters["@vfec_com"].Value = _barcoBE.Fec_com;
                cmd.Parameters.Add(new SqlParameter("@vtip_bar", SqlDbType.VarChar, 1));
                cmd.Parameters["@vtip_bar"].Value = _barcoBE.Tip_bar;
                cmd.Parameters.Add(new SqlParameter("@vcapacidad", SqlDbType.Int));
                cmd.Parameters["@vcapacidad"].Value = _barcoBE.Capacidad;
                cmd.Parameters.Add(new SqlParameter("@vfec_ult_man", SqlDbType.SmallDateTime));
                cmd.Parameters["@vfec_ult_man"].Value = _barcoBE.Fec_ult_man;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;
        }

        public bool ActualizarBarco(BarcoBE _barcoBE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_ActualizarBarco";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vcod_bar", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_bar"].Value = _barcoBE.Cod_bar;
                cmd.Parameters.Add(new SqlParameter("@vnom_bar", SqlDbType.VarChar, 15));
                cmd.Parameters["@vnom_bar"].Value = _barcoBE.Nom_bar;
                cmd.Parameters.Add(new SqlParameter("@vfec_com", SqlDbType.SmallDateTime));
                cmd.Parameters["@vfec_com"].Value = _barcoBE.Fec_com;
                cmd.Parameters.Add(new SqlParameter("@vtip_bar", SqlDbType.VarChar, 1));
                cmd.Parameters["@vtip_bar"].Value = _barcoBE.Tip_bar;
                cmd.Parameters.Add(new SqlParameter("@vcapacidad", SqlDbType.Int));
                cmd.Parameters["@vcapacidad"].Value = _barcoBE.Capacidad;
                cmd.Parameters.Add(new SqlParameter("@vfec_ult_man", SqlDbType.SmallDateTime));
                cmd.Parameters["@vfec_ult_man"].Value = _barcoBE.Fec_ult_man;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;
        }


        public bool EliminarBarco(String vcod_bar)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_EliminarBarco";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vcod_bar", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_bar"].Value = vcod_bar;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;

        }

        public BarcoBE ConsultarBarco(string vcod_bar)
        {
            BarcoBE _barcoBE = new BarcoBE();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ConsultarBarco";
                //Agregamos el parametro para el SP
                cmd.Parameters.Add(new SqlParameter("@vcod_bar", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_bar"].Value = vcod_bar;
                cnx.Open();
                dtr = cmd.ExecuteReader();
                if (dtr.HasRows == true)
                {
                    dtr.Read();
                    var _mibarco = _barcoBE;
                    _mibarco.Cod_bar = dtr.GetValue(dtr.GetOrdinal("cod_bar")).ToString();
                    _mibarco.Nom_bar = dtr.GetValue(dtr.GetOrdinal("nom_bar")).ToString();
                    _mibarco.Fec_com = Convert.ToDateTime(dtr.GetValue(dtr.GetOrdinal("fec_com")));
                    _mibarco.Tip_bar = dtr.GetValue(dtr.GetOrdinal("tip_bar")).ToString();
                    _mibarco.Capacidad = Convert.ToInt16(dtr.GetValue(dtr.GetOrdinal("capacidad")));
                    _mibarco.Fec_ult_man = Convert.ToDateTime(dtr.GetValue(dtr.GetOrdinal("fec_ult_man")));
                    
                    dtr.Close();
                }
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                cmd.Parameters.Clear();
            }
            return _barcoBE;
        }

    }
}
